package com.liner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liner.entity.ProductCategory;
import com.liner.entity.ProductInfo;
import com.liner.mapper.ProductCategoryMapper;
import com.liner.mapper.ProductInfoMapper;
import com.liner.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liner.vo.BuyerProductCategoryVo;
import com.liner.vo.BuyerProductInfoVo;
import com.liner.vo.SellerProductCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 类目表 服务实现类
 * </p>
 *
 * @author liner
 * @since 2022-12-03
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;


    @Override
    public List<BuyerProductCategoryVo> buyerList() {
        //TODO 模仿后期优化

        //先查询类目表
        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
        //封装到 BuyerProductCategoryVo
        List<BuyerProductCategoryVo> buyerList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            BuyerProductCategoryVo buyerProductCategoryVo = new BuyerProductCategoryVo();
           BeanUtils.copyProperties(productCategory,buyerProductCategoryVo);
            LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductInfo::getCategoryType,productCategory.getCategoryType());
            //查询商品表
            List<ProductInfo> productInfoList = productInfoMapper.selectList(queryWrapper);
            //封装到 BuyerProductInfoVo
            List<BuyerProductInfoVo> buyerProductInfoVos = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                BuyerProductInfoVo buyerVo = new BuyerProductInfoVo();
                BeanUtils.copyProperties(productInfo, buyerVo);
                buyerProductInfoVos.add(buyerVo);
            }
            buyerProductCategoryVo.setGoods(buyerProductInfoVos);
            buyerList.add(buyerProductCategoryVo);
        }
        return buyerList;
    }

    @Override
    public List<SellerProductCategoryVo> sellerList() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
        //封装到 SellerProductCategoryVo
        List<SellerProductCategoryVo> sellerList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            SellerProductCategoryVo  sellerProductCategoryVo= new SellerProductCategoryVo();
            sellerProductCategoryVo.setName(productCategory.getCategoryName());
            sellerProductCategoryVo.setType(productCategory.getCategoryType());
            sellerList.add(sellerProductCategoryVo);
        }

        return sellerList;
    }
}
